<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商品列表</title>
    <script>
        function doDelet(flag){
            var deletes=document.querySelectorAll('button');
            // console.log(deletes);
           var deleteID=flag.parentNode.parentNode.children[0].innerText;
           console.log(deleteID);
           location.href="selectProducts.php?data="+deleteID;            
        }
    </script>
    <style>
        .container{
            width: 1200px;
            margin:20px auto;
        }
    </style>
<body>
    <div class="container">
        <div style="text-align: right;"><?php print_r("欢迎登陆：{$_SESSION['loginUser']['su_nickname']}")?></div>
        <form action="./selectProducts.php" method="post">
        <div>
            <label>表名: <input type="text" name="tablename" value=""></label>
        </div>
        <div>
            <label> I D : <input type="text" name="content" value=""></label>
        </div>
        <div style="margin:10px 0 0 20px">
            <button>查询</button>
            <button onclick="window.open('./updata.php')">修改</button>
            <button onclick="window.open('./add.php')">添加</button>
        </div>
        </form>
        <hr>
        <?php 
         $tablename=$_REQUEST['tablename'];
         $content=(int)$_REQUEST['content'];
        if($tablename!=null){?>
        <h2 style="text-align:center">商品列表</h2>
        <table border="1" cellpadding="5" cellspacing="5" align="center">
            <thead>
                <tr>
                    <?php
                    require_once("./DButil.php");
                    $sql="select * from {$tablename}";
                    $resultArr= query(getConn(), $sql);
                    if($resultArr['success']){
                        $product=$resultArr['data'];
                        $p=$product[0];
                            foreach($p as $key => $value){
                                echo "<th>$key</th>";
                            }
                        }                    
                    ?>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    foreach($product as $values){
                        echo "<tr>";                       
                                foreach($values as $valuess){
                                    echo "<td>$valuess</td>";
                        }
                        echo "<td><button onclick='doDelet(this)'>删除</button></td>";
                        echo "</tr>";
                    }
                ?>
            </tbody>
        </table>
        <?}?>
    </div>
</body>
</html>